import urllib.request
import datetime
import time
import json
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import datetime




lastlist = []
td = datetime.datetime.now().strftime('%Y-%m-%d')

#'Accept': 'application/json, text/plain, */*'
#'Accept-Encoding': 'gzip, deflate'
#'Accept-Language': zh-CN,zh;q=0.9,en;q=0.8'
#'Authorization': 'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3MSwidXNlcm5hbWUiOiIwMTM4MzMxNyIsImRpc3BsYXlfbmFtZSI6Ilx1NTIxOFx1NTYwOVx1NGYxZigwMTM4MzMxNykiLCJyb2xlIjpbImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU4MDE3OTAsImVtYWlsIjoiIiwib3JpZ19pYXQiOjE1NjU3OTQ1OTB9.ouh6CvNiyEdPVf5DsZjuUiT-xMwFQWWXyU3pQPJuTHI'
#'Connection': 'keep-alive'
#'Cookie': 'token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3MSwidXNlcm5hbWUiOiIwMTM4MzMxNyIsImRpc3BsYXlfbmFtZSI6Ilx1NTIxOFx1NTYwOVx1NGYxZigwMTM4MzMxNykiLCJyb2xlIjpbImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU4MDE3OTAsImVtYWlsIjoiIiwib3JpZ19pYXQiOjE1NjU3OTQ1OTB9.ouh6CvNiyEdPVf5DsZjuUiT-xMwFQWWXyU3pQPJuTHI; Admin-Token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3MSwidXNlcm5hbWUiOiIwMTM4MzMxNyIsImRpc3BsYXlfbmFtZSI6Ilx1NTIxOFx1NTYwOVx1NGYxZigwMTM4MzMxNykiLCJyb2xlIjpbImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU4MDE3OTAsImVtYWlsIjoiIiwib3JpZ19pYXQiOjE1NjU3OTQ1OTB9.ouh6CvNiyEdPVf5DsZjuUiT-xMwFQWWXyU3pQPJuTHI'
#'DNT': '1'
#'Host': 'sops.sf-express.com'
#'Referer': 'http://sops.sf-express.com/'
#'User-Agen't: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
def spdierweb():
    td = datetime.datetime.now().strftime('%Y-%m-%d')
    tm = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    
    #urla="http://10.116.216.54/Deployment/dateRequest?date1=2019-08-13+08:30:00&date2=2019-08-14+08:30:00"
    
    urla="http://10.116.216.54/Deployment/dateRequest?date1=%s+08:30:00&date2=%s+08:30:00" % (td, tm)
    #cookie="token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3MSwidXNlcm5hbWUiOiIwMTM4MzMxNyIsImRpc3BsYXlfbmFtZSI6Ilx1NTIxOFx1NTYwOVx1NGYxZigwMTM4MzMxNykiLCJyb2xlIjpbImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU4MDE3OTAsImVtYWlsIjoiIiwib3JpZ19p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ImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU5MjA3ODUsImVtYWlsIjoiIiwib3JpZ19pYXQiOjE1NjU5MTM1ODV9.48cV2-q9A9aOlB2ytEmLN6ThAVEnZ_K3KIGKWAD_87w"
    request_header={
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Authorization': 'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3MSwidXNlcm5hbWUiOiIwMTM4MzMxNyIsImRpc3BsYXlfbmFtZSI6Ilx1NTIxOFx1NTYwOVx1NGYxZigwMTM4MzMxNykiLCJyb2xlIjpbImFkbWluIl0sImdyb3VwIjpbXSwiaXNfc3VwZXJ1c2VyIjpmYWxzZSwiaXNfYWN0aXZlIjp0cnVlLCJhdXRoX3R5cGUiOiJjYXMiLCJob3N0X3doaXRlIjpudWxsLCJleHAiOjE1NjU4MDE3OTAsImVtYWlsIjoiIiwib3JpZ19pYXQiOjE1NjU3OTQ1OTB9.ouh6CvNiyEdPVf5DsZjuUiT-xMwFQWWXyU3pQPJuTHI',
        'Connection': 'keep-alive',
        'Cookie': cookie,
        'DNT': '1',
        'Host': 'sops.sf-express.com',
        'Referer': 'http://sops.sf-express.com/',
        "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    }
     
    #params = {
    #  "message":"success","code":20000,"data":{"username":"01383317","display_name":"刘嘉伟(01383317)","department":"TKJ7551.顺丰科技IT运营中心IT运营平台部部署服务组","email":"","is_superuser":"false","is_active":"true","role":["admin"],"group":[],"auth_type":"cas","token":"null","token_type":"short","last_login":"2019-08-14 22:56:30","date_joined":"2019-08-05 14:14:55"}
    #  }
    #time.sleep(1)
    #dataa = bytes(urllib.parse.urlencode(params), encoding='utf8')
    #time.sleep(1)
    #req=urllib.request.Request(url = urla, headers=request_header)
    #time.sleep(1)
    #resp=urllib.request.urlopen(req)
    #data=resp.read().decode('utf-8')
    #2019年8月17日22:59:38
    req=urllib.request.Request(url = urla, headers=request_header)
    resp=urllib.request.urlopen(req)
    b = (resp.read()).decode('utf-8')
    dataa = json.loads(b)#list
     
    for i in range(len(dataa)):
        if dataa[i]["name_prd"] == "刘嘉伟":
            lastlist.append(dataa[i])
    
    
    strlist = (" ".join('%s' %id for id in lastlist))
    
    data = strlist.encode()
    datb = b.encode()	
    #with open('C:/Users/01383317/spider-ezdeploy/ljwdingshi/youdeploy.json','wb') as fpa:
    #    fpa.write(data)
    with open('./youdeploy.json','wb') as fpa:
        fpa.write(data)
    with open('./todaydeploy.json','wb') as fpb:
        fpb.write(datb)
    print("SPIDER SUCCESSFUL")		


def Sendto(): 
    # 第三方 SMTP 服务
    mail_host="mail.SFMAIL.SF-EXPRESS.COM"        #设置服务器
    mail_user="JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM" #用户名
    mail_pass="Wwwljw.com"                        #口令 
     
     
    sender = 'JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM'
    receivers = ['jiawei.liu4@sfmail.sf-express.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
     
    #message = MIMEText('Python 邮件发送测试-MIMEText...', 'plain', 'utf-8')
    #message['From'] = Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')
    #message['To'] =  Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')
    #subject = ''
    #message['Subject'] = Header(subject, 'utf-8')
    
    
    #创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')#显示的发件人
    message['To'] =  Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8') #显示的收件人
    subject = 'Python-SMTP邮件:TITLE这是今天你的发布任务'
    message['Subject'] = Header(subject, 'utf-8')
     
    #邮件正文内容
    message.attach(MIMEText('这是今天你的发布:16点时采集的任务,一份全量,一份自己的', 'plain', 'utf-8')) 
    #构造附件1，传送当前目录下的文件
    att1 = MIMEText(open('youdeploy.json', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="youdeploy.txt"'    #这里的filename可以任意写，写什么名字，邮件中显示什么名字
    message.attach(att1)
    #构造附件2
    att2 = MIMEText(open('todaydeploy.json', 'rb').read(), 'base64', 'utf-8') 
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="todaydeploy.txt"'    #这里的filename可以任意写，写什么名字，邮件中显示什么名字
    message.attach(att2)
    
    
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
        smtpObj.login(mail_user,mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")

 
if __name__ == '__main__':
    spdierweb()
    #print ("执行到if__name")
    #time.sleep(10)
    #Sendtomail()
    



